import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import '../../helper/uidata.dart';
import '../../model/calculate.dart';
import '../../widgets/TimeLinePanel3.dart';
import '../../model/dc_chartval.dart';

import '../../widgets/TimeLinePanel2.dart';

import '../../helper/Helper.dart';
import '../../helper/channelHelper.dart';
import '../../widgets/inputWidget.dart';

const ShowColor1 = Colors.blue;
const ShowColor2 = Colors.blue;

class CalculatePage extends StatefulWidget {
  @override
  CalculatePage({this.ifAppBar = false});
  final ifAppBar;
  @override
  CalculatePageState createState() => new CalculatePageState();
}

class CalculatePageState extends State<CalculatePage> {
  List<Calculate>? fdList;

  bool ifLoaded = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    LoadData();
  }

  void LoadData() async {
    fdList = await Calculate.GetNew();
    //UIData.SkData1=sk;
    print(fdList);
    setState(() {
      ifLoaded = true;
    });
    ChannelHelper.showToast("更新完成!");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: getFloatingButton(context),
      body: CalculateBody(context),
    );
  }

  Widget getFloatingButton(BuildContext context) {
    return new FloatingActionButton(
      child: Icon(Icons.refresh),
      onPressed: () {
        LoadData();
      },
    );
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list5 = <Widget>[];
    List<Widget> list6 = <Widget>[];

    //list1.add(InputWidget.getGreyInfo("水库实时数据", Helper.DateTimeFormat( sk.TM)));

    // var v1=InputWidget.getShowInfo("入库流量", sk.R.toStringAsFixed(0), ShowColor1);
    // var v2=InputWidget.getShowInfo("出库流量", (sk.Qf+sk.Qp).toStringAsFixed(0), ShowColor1);

    list1.add(InputWidget.inCard3(list3));
    list1.add(Container(
      height: 36.0,
      child: Text(""),
    ));

    list1.add(InputWidget.inCard(getLinePanel()));

    list1.add(Container(
      height: 56.0,
      child: Text(""),
    ));

    return list1;
  }

  Widget getLinePanel() {
    var t1 = <String>[];
    var v1 = <double>[];
    var v2 = <double>[];
    var v3 = <double>[];
    var v4 = <double>[];

    for (var x1 in fdList!) {
      t1.add(Helper.DateTimeFormat2(x1.TM));

      v1.add(x1.H);
      v2.add(x1.R);
      v3.add(x1.QZ);
      v4.add(x1.QP);
    }

    return TimeLinePanel3("优化调度曲线", t1, [v1, v2, v3, v4],
        ["水位", "入库", "出库", "泄洪"], 0, 2000, 1, "m、m3/s");
  }

  CalculateBody(BuildContext context) => new Scaffold(
      appBar: widget.ifAppBar
          ? AppBar(
              // title: bottomTabs[currentIndex].title,
              title: Text("发电实时数据"))
          : null,
      body: SingleChildScrollView(
        child: CalculateFields(context),
      ));

  CalculateFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
